/* eslint-disable default-case */
import { message } from 'antd';
import {
  handleOkSuc,
  cancleApply,
} from '@/services/reduceempaccu';
import * as service from '@/services/socket';

export default {
  state: {
    resultInfo: {
        // 结果数据必传
        batchName: '', // 任务名称
        taskCount: 0, // 导入数据
        successCount: 0, // 成功数据
        errorCount: 0, // 失败数据
        batchNo: '', // 批次号
        errorUrl: '/osApi/accuDeduDet/syncAccuDeduDeclErrMessage', // 下载错误信息地址
        errorParams: null, // 下载错误数据参数
    },
    showProgress: false,
    progressPercent: 0,
  },
  effects: {
    // 上传文件:批量进度
    *uploadFileWithProgress({ payload: { params, type } }, { call, put }) {   
       // type === '1' 是标记申报成功 '2'为取消申报
        const response = type === '1' ? yield call(handleOkSuc, params) : yield call(cancleApply, params);
        yield put({
          type: 'updateModelData',
          payload: {
            progressPercent: 0,
          },
        });
        if (response.success) {
            yield put({
                type: 'updateModelData',
                payload: {
                    batchId: response.data.batchId,
                    progressPercent: 0,
                },
            });
            if (response.data && response.data.successProp !== 100) {
                yield put({
                    type: 'updateModelData',
                    payload: {
                        showProgress: true,
                    },
                });
                yield call(service.socketChannel, {
                    type: 'sub',
                    data: { to: response.data && (response.data.batchId || response.data), type: 'task' },
                });
            } else {
                yield put({
                    type: 'updateBatchTask',
                    payload: response.data,
                });
            }
        } else {
           // 上传失败
           message.warning(`${response.message || '服务器端错误'}`);
        }
        return response;
    },

    // 匹配唯一batchId
    *matchBatchId({ payload }, { put, select }) {
      const { batchId } = yield select(state => state.reduceaccubatch);
      if (batchId === payload.batchId) {
        yield put({
          type: 'updateBatchTask',
          payload,
        });
      }
    },
  },

  reducers: {
    // 更新数据
    updateModelData(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },

    // 更新批量任务数据
    updateBatchTask(state, { payload }) {
      let { showProgress, resultInfo } = state;
      showProgress = true;
      if (payload.successProp === 100) {
        resultInfo.errorParams = {
          batNo: payload.batchId,
        };
        resultInfo = { ...resultInfo, ...payload };
      }
      return {
        ...state,
        showProgress,
        progressPercent: payload.successProp,
        resultInfo,
      };
    },
  },
  subscriptions: {
    socket({ dispatch }) {
      return service.listen(action => {
        switch (action.type) {
          case 'task':
            dispatch({
              type: 'matchBatchId',
              payload: action.data,
            });
            // 全局保存
            dispatch({
              type: 'publicdata/updateBatchTaskData',
              payload: action.data,
            });
            break;
        }
      });
    },
  },
};
